package libnetdb.dm.assoc.dhp.demo;


import java.text.DecimalFormat;

import libnetdb.dm.assoc.base.*;
import libnetdb.dm.assoc.util.*;

/**
 * RulePrinter will print rules to the standard output stream.
 * This class implements the <code>RuleCollector</code> interface
 * so that it can be used to collect rules from a rule set.
 *
 * @author Cheng-Ru Lin
 * @version 1.0
 */
public class RulePrinter implements RuleCollector {
    private NameSpace ns;
    static private DecimalFormat format = new DecimalFormat("0.000");

    public RulePrinter(NameSpace ns) {
        this.ns = ns;
    }

    public boolean addRule(Rule rule) {
        String supp = format.format(rule.getSupport()),
                 conf = format.format(rule.getConfidence()),
                 inte = format.format(rule.getInteresting());
                 
        System.out.println(translate(rule.getCause().ids) + " ==> "
                + translate(rule.getConsequence().ids) + " (" + supp
                + ", " + conf + ", " + inte + ")");
        return true;
    }

    private String translate(int ids[]){
        StringBuffer strbuf = new StringBuffer("[");
        if(ids.length > 0)
            strbuf.append(ns.getName(ids[0]));
        for(int i=1;i<ids.length;++i)
            strbuf.append(", ").append(ns.getName(ids[i]));
        strbuf.append("]");
        return strbuf.toString();
    }
}
